﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class CalendarGroupBLL : IBaseInfo
    {
        private static readonly ICalendarGroupDA CalendarGroupDA = DataAccess.CreateCalendarGroupDA();
        private static readonly ICalendarGroupDetailDA CalendarGroupDetailDA = DataAccess.CreateCalendarGroupDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ITariffDA tariffDA = DataAccess.CreateTariffDA();

        public CalendarGroupBLL()
        {
            this.pubParasInfo = null;
        }

        public CalendarGroupBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CalendarGroupBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        private void Copy(string strContractID, CalendarGroupInfo calendarGroupInfo)
        {
            if ((calendarGroupInfo.CalendarGroupDetailInfos != null) && (calendarGroupInfo.CalendarGroupDetailInfos.Count > 0))
            {
                foreach (CalendarGroupDetailInfo CGI in calendarGroupInfo.CalendarGroupDetailInfos)
                {
                    CGI.CalendarGroupID = strContractID;
                    CGI.CreatedBy = this.pubParasInfo.UserID;
                    CalendarGroupDetailDA.InsertCalendarGroupDetail(CGI, this.dataBaseID[0]);
                }
            }
        }

        public string Delete(List<string> calendarGroupIDS)
        {
            int i;
            string temp = string.Empty;
            string tariffRateIDS = string.Empty;
            foreach (string id in calendarGroupIDS)
            {
                List<TariffInfo> tariffListInfo = tariffDA.GetTariffByByTypeCode("CalendarGroup", id, this.dataBaseID[0]);
                if ((tariffListInfo != null) && (tariffListInfo.Count > 0))
                {
                    for (i = 0; i < tariffListInfo.Count; i++)
                    {
                        tariffRateIDS = tariffRateIDS + tariffListInfo[i].TariffID + ";";
                    }
                    if (!string.IsNullOrEmpty(tariffRateIDS))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F9140_005", this.pubParasInfo.UserLanguage, new List<string> { tariffRateIDS.Substring(0, tariffRateIDS.Length - 1) });
                    }
                }
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<CalendarGroupInfo> calendarGroupList = new List<CalendarGroupInfo>();
                CalendarGroupInfo calendarGroupInfo = null;
                foreach (string id in calendarGroupIDS)
                {
                    calendarGroupInfo = CalendarGroupDA.GetCalendarGroupByID(dataBase, tran, id, this.dataBaseID[0]);
                    calendarGroupInfo.CalendarGroupDetailInfos = CalendarGroupDetailDA.GetCalendarGroupDetailByID(dataBase, tran, id, this.dataBaseID[0]);
                    calendarGroupList.Add(calendarGroupInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                foreach (string id in calendarGroupIDS)
                {
                    CalendarGroupDetailDA.DeleteCalendarGroupDetail(id, this.dataBaseID[0]);
                }
                CalendarGroupDA.DeleteCalendarGroup(dataBase, tran, calendarGroupIDS, this.dataBaseID[0]);
                for (i = 0; i < calendarGroupIDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<CalendarGroupInfo>(dataBase, tran, calendarGroupList[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "CALENDAR_GROUP", this.dataBaseID[0]);
                    if ((calendarGroupList[i].CalendarGroupDetailInfos != null) && (calendarGroupList[i].CalendarGroupDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < calendarGroupList[i].CalendarGroupDetailInfos.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<CalendarGroupDetailInfo>(dataBase, tran, calendarGroupList[i].CalendarGroupDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "CALENDAR_GROUP_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + calendarGroupIDS[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return CalendarGroupDA.GetAllCalendarGroup(this.dataBaseID[0]);
        }

        public object GetInfoByID(string calendarGroupID)
        {
            CalendarGroupInfo calendarGroupInfo = CalendarGroupDA.GetCalendarGroupByID(calendarGroupID, this.dataBaseID[0]);
            if (calendarGroupInfo != null)
            {
                calendarGroupInfo.CalendarGroupDetailInfos = CalendarGroupDetailDA.GetCalendarGroupDetailByID(calendarGroupID, this.dataBaseID[0]);
            }
            return calendarGroupInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            CalendarGroupQueryEntity calendarGroupQuery = obj as CalendarGroupQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(calendarGroupQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + calendarGroupQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(calendarGroupQuery.CalendarGroupID))
            {
                sqlWhere.Add(" CALENDAR_GROUP_ID " + calendarGroupQuery.CalendarGroupID);
            }
            if (!string.IsNullOrEmpty(calendarGroupQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + calendarGroupQuery.Descr);
            }
            return CalendarGroupDA.GetCalendarGroupByQueryList(sqlWhere, calendarGroupQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            CalendarGroupInfo calendarGroupInfo = obj as CalendarGroupInfo;
            string calendarGroupID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(calendarGroupInfo.CalendarGroupID))
                {
                    calendarGroupInfo.CalendarGroupID = commonMethod.GenNextNumStr(dataBase, tran, " CALENDAR_GROUP", this.dataBaseID[0]);
                }
                calendarGroupInfo.UpdatedBy = calendarGroupInfo.CreatedBy;
                if (CalendarGroupDA.InsertCalendarGroup(dataBase, tran, calendarGroupInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CalendarGroupInfo>(dataBase, tran, null, calendarGroupInfo, AuditAction.Insert, this.pubParasInfo.UserID, "CALENDAR_GROUP", this.dataBaseID[0]);
                    calendarGroupID = calendarGroupInfo.CalendarGroupID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            this.Copy(calendarGroupID, calendarGroupInfo);
            return calendarGroupID;
        }

        public string Update(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string calendarGroupID = string.Empty;
            try
            {
                CalendarGroupInfo calendarGroup = obj as CalendarGroupInfo;
                CalendarGroupInfo oldCalendarGroupInfo = CalendarGroupDA.GetCalendarGroupByID(dataBase, tran, calendarGroup.CalendarGroupID, this.dataBaseID[0]);
                if (CalendarGroupDA.UpdateCalendarGroup(dataBase, tran, calendarGroup, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CalendarGroupInfo>(dataBase, tran, oldCalendarGroupInfo, calendarGroup, AuditAction.Update, this.pubParasInfo.UserID, "AREA", this.dataBaseID[0]);
                    calendarGroupID = calendarGroup.CalendarGroupID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return calendarGroupID;
        }
    }
}

